package give.my.money.back;

	import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

	import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

	public class TabDebtorsList extends ListActivity{
	
		 private MySimpleCursorAdapter myAdapter;
		 private Cursor c;
		    public void onCreate(Bundle savedInstanceState) {
		        super.onCreate(savedInstanceState);
		        Log.d("ONCREATE", "estoy dentro");
		        //Open DB in write mode.
		        DeudasSQLiteHelper databaseHelper = new DeudasSQLiteHelper(this, null,3);
				SQLiteDatabase db = databaseHelper.getWritableDatabase();
				
				//The query that we want is : "select Name, sum(quantity) from test.contacts group by Name_id"
				//Lo que voy a coger
				String[] a = new String[] {"sum(quantity)",databaseHelper.NAME,databaseHelper._ID};
				
				//There are two valid ways, using db.query or db.rawQuery both works!
				c = db.query(databaseHelper.TDEUDAS,a, null, null,databaseHelper._ID,null, databaseHelper.NAME + " ASC");	
				//Cursor co = db.rawQuery("select sum(quantity),name,_id from deudas group by _id ORDER BY name ASC",null);
		        
				startManagingCursor(c);        
				Log.d("GMMB", c.getColumnName(0));
				
		        // start mappings, column datas with view items.
			    String[] columns = new String[] {databaseHelper.NAME, c.getColumnName(0), databaseHelper._ID };
			    //Ids for to link with the view items (TextView,TextView,ImageView)
		        int[] resources = new int[] {R.id.contact_name, R.id.total_quantity, R.id.imageView1};
		        
		        myAdapter = new MySimpleCursorAdapter(this,R.layout.debtors,c,columns,resources);
		        setListAdapter(myAdapter);
	        
		        
		        ListView lv = getListView();
		        lv.setTextFilterEnabled(true);

		        lv.setOnItemClickListener(new OnItemClickListener() {
		  	    
		  		public void onItemClick(AdapterView<?> parent, android.view.View view,
		  				int position, long id) {
		  			// TODO Auto-generated method stub
		  		     // When clicked, show a toast with the TextView text
		  		      Toast.makeText(getApplicationContext(), "Id:"+String.valueOf(id) +" Position:" + String.valueOf(position)
		  		    		  ,Toast.LENGTH_SHORT).show();
		  		   //Show the Debt view of the selected contact 
		  		   TabDebtorsList.this.showUserInformationActivity(id);
	 		      
		  		}
		  	  });
		        
		       
		    }
		
		  //Show the Debt view of the selected contact
		    private  void showUserInformationActivity(long id){
		  		 // Esto para realizar la llamada
	  		    Intent i= new Intent().setClass(this, UserInformationListActivity.class);
	  		    i.putExtra("contactID",String.valueOf(id));
	  		    Log.d("GMMB","Valor pasado a user_information_activity:" + id);
	  		    startActivity(i);
		    	
		    }
		    
		    
		    public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
		        Uri uri = ContentUris.withAppendedId(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
		        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
		        if (input == null) {
		             return null;
		         }
		        return BitmapFactory.decodeStream(input);
		    }
		    
		 
	}
